/*
 * File:    avrnacl_8bitc/crypto_hashblocks/sha512.c
 * Author:  Michael Hutter, Peter Schwabe
 * Version: Wed Aug 6 13:19:40 2014 +0200
 * Public Domain
 */

#include "avrnacl.h"
#include "bigint.h"

typedef struct{
  unsigned char v[8];
} myu64;

static void myu64_load_bigendian(myu64 *r, const unsigned char *x)
{
  r->v[7] = x[0];
  r->v[6] = x[1];
  r->v[5] = x[2];
  r->v[4] = x[3];
  r->v[3] = x[4];
  r->v[2] = x[5];
  r->v[1] = x[6];
  r->v[0] = x[7];
}

static void myu64_store_bigendian(unsigned char *r, const myu64 *x)
{
  r[7] = x->v[0];
  r[6] = x->v[1];
  r[5] = x->v[2];
  r[4] = x->v[3];
  r[3] = x->v[4];
  r[2] = x->v[5];
  r[1] = x->v[6];
  r[0] = x->v[7];
}


static void myu64_add(myu64 *r, const myu64 *x, const myu64 *y)
{
  bigint_add(r->v, x->v, y->v, 8);
}

static void myu64_xor(myu64 *r, const myu64 *x, const myu64 *y)
{
  unsigned char i;
  for(i=0;i<8;i++)
    r->v[i] = x->v[i] ^ y->v[i];
}

static void myu64_and(myu64 *r, const myu64 *x, const myu64 *y)
{
  unsigned char i;
  for(i=0;i<8;i++)
    r->v[i] = x->v[i] & y->v[i];
}

static void myu64_not(myu64 *r, const myu64 *x)
{
  unsigned char i;
  for(i=0;i<8;i++)
    r->v[i] = ~x->v[i];
}

static void myu64_ror(myu64 *r, unsigned char n)
{
  myu64 t = *r;
  unsigned char u = n >> 3;
  unsigned char l = n & 7;
  unsigned char i;
  for(i=0;i<8;i++)
    r->v[i] = (t.v[(i+u+1)&7] << (8-l)) | (t.v[(i+u)&7] >> l);
}

// Works for values 6 and 7 as required, does not work generally
static void myu64_shr(myu64 *r, unsigned char n)
{
  unsigned char i;
  for(i=0;i<7;i++)
    r->v[i] = (r->v[i] >> n) | (r->v[i+1] << (8-n));
  r->v[7] >>= n;
}

static void Ch(myu64 *r, const myu64 *x, const myu64 *y, const myu64 *z)
{
  myu64 t;
  myu64_and(&t,x,y);
  myu64_not(r,x);
  myu64_and(r,r,z);
  myu64_xor(r,r,&t);
}

static void Maj(myu64 *r, const myu64 *x, const myu64 *y, const myu64 *z)
{
  myu64 t0;
  myu64 t1;
  myu64_and(&t0,x,y);
  myu64_and(&t1,x,z);
  myu64_and(r,y,z);
  myu64_xor(r,r,&t0);
  myu64_xor(r,r,&t1);
}

static void Sigma0(myu64 *r, const myu64 *x)
{
  *r = *x;
  myu64 t = *x;
  myu64_ror(r, 28);
  myu64_ror(&t, 34);
  myu64_xor(r, r, &t);
  myu64_ror(&t, 5);
  myu64_xor(r, r, &t);
}

static void Sigma1(myu64 *r, const myu64 *x)
{
  *r = *x;
  myu64 t = *x;
  myu64_ror(r, 14);
  myu64_ror(&t, 18);
  myu64_xor(r, r, &t);
  myu64_ror(&t, 23);
  myu64_xor(r, r, &t);
}

static void sigma0(myu64 *r, const myu64 *x)
{
  *r = *x;
  myu64 t0 = *x;
  myu64 t1 = *x;
  myu64_ror(r, 1);
  myu64_ror(&t0, 8);
  myu64_xor(r, r, &t0);
  myu64_shr(&t1, 7);
  myu64_xor(r, r, &t1);
}

static void sigma1(myu64 *r, const myu64 *x)
{
  *r = *x;
  myu64 t0 = *x;
  myu64 t1 = *x;
  myu64_ror(r, 19);
  myu64_ror(&t0, 61);
  myu64_xor(r, r, &t0);
  myu64_shr(&t1, 6);
  myu64_xor(r, r, &t1);
}

static void M(myu64 *w0, const myu64 *w14, const myu64 *w9, const myu64 *w1)
{
  myu64 t0, t1;
  sigma0(&t0, w1);
  sigma1(&t1, w14);
  myu64_add(w0, w0, w9);
  myu64_add(w0, w0, &t0);
  myu64_add(w0, w0, &t1);
}

static void expand(myu64 *w0, myu64 *w1, myu64 *w2, myu64 *w3, myu64 *w4, myu64 *w5, myu64 *w6, myu64 *w7,
                   myu64 *w8, myu64 *w9, myu64 *w10, myu64 *w11, myu64 *w12, myu64 *w13, myu64 *w14, myu64 *w15)
{
  M(w0 ,w14,w9 ,w1 );
  M(w1 ,w15,w10,w2 );
  M(w2 ,w0 ,w11,w3 );
  M(w3 ,w1 ,w12,w4 );
  M(w4 ,w2 ,w13,w5 );
  M(w5 ,w3 ,w14,w6 );
  M(w6 ,w4 ,w15,w7 );
  M(w7 ,w5 ,w0 ,w8 );
  M(w8 ,w6 ,w1 ,w9 );
  M(w9 ,w7 ,w2 ,w10);
  M(w10,w8 ,w3 ,w11);
  M(w11,w9 ,w4 ,w12);
  M(w12,w10,w5 ,w13);
  M(w13,w11,w6 ,w14);
  M(w14,w12,w7 ,w15);
  M(w15,w13,w8 ,w0 );
}

static void myF(myu64 *a, myu64 *b, myu64 *c, myu64 *d, myu64 *e, myu64 *f, myu64 *g, myu64 *h, const myu64 *w, const myu64 *k)
{
  myu64 t, t1, t2;
  Ch(&t, e, f, g);
  Sigma1(&t1, e);
  myu64_add(&t1, &t1, h);
  myu64_add(&t1, &t1, &t);
  myu64_add(&t1, &t1, k);
  myu64_add(&t1, &t1, w);

  Sigma0(&t2, a);
  Maj(&t,a,b,c);
  myu64_add(&t2, &t2, &t);

  *h = *g;
  *g = *f;
  *f = *e;
  myu64_add(e,d,&t1);
  *d = *c;
  *c = *b;
  *b = *a;
  myu64_add(a,&t1,&t2);
}


const myu64 roundconstants[80] = {
{{0x22, 0xae, 0x28, 0xd7, 0x98, 0x2f, 0x8a, 0x42}},
{{0xcd, 0x65, 0xef, 0x23, 0x91, 0x44, 0x37, 0x71}},
{{0x2f, 0x3b, 0x4d, 0xec, 0xcf, 0xfb, 0xc0, 0xb5}},
{{0xbc, 0xdb, 0x89, 0x81, 0xa5, 0xdb, 0xb5, 0xe9}},
{{0x38, 0xb5, 0x48, 0xf3, 0x5b, 0xc2, 0x56, 0x39}},
{{0x19, 0xd0, 0x05, 0xb6, 0xf1, 0x11, 0xf1, 0x59}},
{{0x9b, 0x4f, 0x19, 0xaf, 0xa4, 0x82, 0x3f, 0x92}},
{{0x18, 0x81, 0x6d, 0xda, 0xd5, 0x5e, 0x1c, 0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a, 0x93, 0x47, 0x91, 0xa7, 0xd5}},
{{0x6f, 0x82, 0x03, 0xe0, 0x51, 0x63, 0xca, 0x06}},
{{0x70, 0x6e, 0x0e, 0x0a, 0x67, 0x29, 0x29, 0x14}},
{{0xfc, 0x2f, 0xd2, 0x46, 0x85, 0x0a, 0xb7, 0x27}},
{{0x26, 0xc9, 0x26, 0x5c, 0x38, 0x21, 0x1b, 0x2e}},
{{0xed, 0x2a, 0xc4, 0x5a, 0xfc, 0x6d, 0x2c, 0x4d}},
{{0xdf, 0xb3, 0x95, 0x9d, 0x13, 0x0d, 0x38, 0x53}},
{{0xde, 0x63, 0xaf, 0x8b, 0x54, 0x73, 0x0a, 0x65}},
{{0xa8, 0xb2, 0x77, 0x3c, 0xbb, 0x0a, 0x6a, 0x76}},
{{0xe6, 0xae, 0xed, 0x47, 0x2e, 0xc9, 0xc2, 0x81}},
{{0x3b, 0x35, 0x82, 0x14, 0x85, 0x2c, 0x72, 0x92}},
{{0x64, 0x03, 0xf1, 0x4c, 0xa1, 0xe8, 0xbf, 0xa2}},
{{0x01, 0x30, 0x42, 0xbc, 0x4b, 0x66, 0x1a, 0xa8}},
{{0x91, 0x97, 0xf8, 0xd0, 0x70, 0x8b, 0x4b, 0xc2}},
{{0x30, 0xbe, 0x54, 0x06, 0xa3, 0x51, 0x6c, 0xc7}},
{{0x18, 0x52, 0xef, 0xd6, 0x19, 0xe8, 0x92, 0xd1}},
{{0x10, 0xa9, 0x65, 0x55, 0x24, 0x06, 0x99, 0xd6}},
{{0x2a, 0x20, 0x71, 0x57, 0x85, 0x35, 0x0e, 0xf4}},
{{0xb8, 0xd1, 0xbb, 0x32, 0x70, 0xa0, 0x6a, 0x10}},
{{0xc8, 0xd0, 0xd2, 0xb8, 0x16, 0xc1, 0xa4, 0x19}},
{{0x53, 0xab, 0x41, 0x51, 0x08, 0x6c, 0x37, 0x1e}},
{{0x99, 0xeb, 0x8e, 0xdf, 0x4c, 0x77, 0x48, 0x27}},
{{0xa8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x77, 0xdb, 0x28}},
{{0x93, 0x24, 0xc7, 0x40, 0x7b, 0xab, 0xca, 0x32}},
{{0xbc, 0xbe, 0xc9, 0x15, 0x0a, 0xbe, 0x9e, 0x3c}},
{{0x4c, 0x0d, 0x10, 0x9c, 0xc4, 0x67, 0x1d, 0x43}},
{{0xb6, 0x42, 0x3e, 0xcb, 0xbe, 0xd4, 0xc5, 0x4c}},
{{0x2a, 0x7e, 0x65, 0xfc, 0x9c, 0x29, 0x7f, 0x59}},
{{0xec, 0xfa, 0xd6, 0x3a, 0xab, 0x6f, 0xcb, 0x5f}},
{{0x17, 0x58, 0x47, 0x4a, 0x8c, 0x19, 0x44, 0x6c}}};

int crypto_hashblocks_sha512(
    unsigned char *statebytes,
    const unsigned char *in,crypto_uint16 inlen
    )
{
  myu64 state[8];
  myu64 a;
  myu64 b;
  myu64 c;
  myu64 d;
  myu64 e;
  myu64 f;
  myu64 g;
  myu64 h;
  unsigned char i;

  myu64 w[16];

  myu64_load_bigendian(&a, statebytes +  0); state[0] = a;
  myu64_load_bigendian(&b, statebytes +  8); state[1] = b;
  myu64_load_bigendian(&c, statebytes + 16); state[2] = c;
  myu64_load_bigendian(&d, statebytes + 24); state[3] = d;
  myu64_load_bigendian(&e, statebytes + 32); state[4] = e;
  myu64_load_bigendian(&f, statebytes + 40); state[5] = f;
  myu64_load_bigendian(&g, statebytes + 48); state[6] = g;
  myu64_load_bigendian(&h, statebytes + 56); state[7] = h;

  while (inlen >= 128) 
  {
    for(i=0;i<16;i++)
      myu64_load_bigendian(w+i, in + 8*i);

    for(i=0;i<16;i++)
      myF(&a, &b, &c, &d, &e, &f, &g, &h, w+i, roundconstants+i);

    expand( w+0,  w+1, w+2, w+3, w+4, w+5, w+6, w+7, w+8, w+9, w+10, w+11, w+12, w+13, w+14, w+15);

    for(i=0;i<16;i++)
      myF(&a, &b, &c, &d, &e, &f, &g, &h, w+i, roundconstants+i+16);

    expand( w+0,  w+1, w+2, w+3, w+4, w+5, w+6, w+7, w+8, w+9, w+10, w+11, w+12, w+13, w+14, w+15);

    for(i=0;i<16;i++)
      myF(&a, &b, &c, &d, &e, &f, &g, &h, w+i, roundconstants+i+32);

    expand( w+0,  w+1, w+2, w+3, w+4, w+5, w+6, w+7, w+8, w+9, w+10, w+11, w+12, w+13, w+14, w+15);

    for(i=0;i<16;i++)
      myF(&a, &b, &c, &d, &e, &f, &g, &h, w+i, roundconstants+i+48);

    expand( w+0,  w+1, w+2, w+3, w+4, w+5, w+6, w+7, w+8, w+9, w+10, w+11, w+12, w+13, w+14, w+15);

    for(i=0;i<16;i++)
      myF(&a, &b, &c, &d, &e, &f, &g, &h, w+i, roundconstants+i+64);

    myu64_add(&a, &a, state+0);
    myu64_add(&b, &b, state+1);
    myu64_add(&c, &c, state+2);
    myu64_add(&d, &d, state+3);
    myu64_add(&e, &e, state+4);
    myu64_add(&f, &f, state+5);
    myu64_add(&g, &g, state+6);
    myu64_add(&h, &h, state+7);

    state[0] = a;
    state[1] = b;
    state[2] = c;
    state[3] = d;
    state[4] = e;
    state[5] = f;
    state[6] = g;
    state[7] = h;

    in += 128;
    inlen -= 128;
  }

  myu64_store_bigendian(statebytes +  0,state+0);
  myu64_store_bigendian(statebytes +  8,state+1);
  myu64_store_bigendian(statebytes + 16,state+2);
  myu64_store_bigendian(statebytes + 24,state+3);
  myu64_store_bigendian(statebytes + 32,state+4);
  myu64_store_bigendian(statebytes + 40,state+5);
  myu64_store_bigendian(statebytes + 48,state+6);
  myu64_store_bigendian(statebytes + 56,state+7);

  return inlen;
}
